/*
 * Written by Farzon Lotfi
 * HW  5
 * PhoneLookup.java
 * Collaboration statement: I worked on this on my own
 * */

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;


/**
 * @author Farzon Lotfi 
 * @version 1.0
 * class that reads in a file  separates it by the first space (ie splits it)
 * int to key and value pair. Then sends it into the MyHashTable.java
 * finally returns a list of values based on key evaluations  
 **/
public class PhoneLookup<K,V> {

 private String keySearch;
 private String file;
 private MyHashTable<String,String> phoneHashTable;
 
 /**
  *  this constructor instantiates all of the field
  *  variables
  *  @param String keySearch - takes in the value you are looking for
  *  @param String file - the file name and location
  **/
 public PhoneLookup(String keySearch, String file, 
   MyHashTable<String,String> phoneHashTable) {
  this.file = file;
  this.keySearch = keySearch.toLowerCase();
  this.phoneHashTable = phoneHashTable;
 }

 
 /**
  *  takes in a string splits it and then sends it to the put method of 
  *  MyHashTable.java
  *  @param  String strCurrent - the current line that the scanner finds
  **/
 private void lineSpliter(String strCurrent) {
  String strVal = strCurrent.substring(0,strCurrent.indexOf(" "));
  String strKey = strCurrent.substring(strCurrent.indexOf(" ")+1,
    strCurrent.length()).toLowerCase();
  phoneHashTable.put(strKey, strVal);
 }

 /**
  *  the scanner method reads in the file line by line and sends it to 
  *  the lineSplitter method for splitting the key and values apart that will
  *  be added to the hash table.
  **/
 private void read() {
  try {
   Scanner scanner = new Scanner(new File(file)); 
   while(scanner.hasNext()) {
    String currentLine = scanner.nextLine();
    // System.out.println(currentLine);
    lineSpliter(currentLine);
   }
  }
  catch (IOException ioe) { 
   System.out.println(ioe); 
  }
 }



 /* private String getAllToString(List<String> list) {
  String listToString = "";
  ListIterator<String> iterator = list.listIterator();
  while(iterator.hasNext()){
   String currentValue = iterator.next();
   listToString +=  currentValue + "\n";
  }
  return listToString;
 }*/
 
 /**
  *  method that calls the read method to write to the hash table
  *  ad then uses the getAll to search for all matching keys and then returns
  *  a list of values of all matching keys
  *  @return List - a list of values
  **/
 public List doItAll() {
  read();
  //System.out.println(phoneHashTable.toString());
  return phoneHashTable.getAll(keySearch);
 }

  /**
  *  reads the doItAll() method and formats
  * the toString into the way you guys wanted it formated
  * 
  *  @return String - returnString: string with correct formating
  **/
 public String StringParse() {
   String parseString = doItAll().toString();
   String returnString = "";
   String[] core = parseString.split(",");
   core[0] = core[0].substring(1,core[0].length());
   core[core.length-1] = core[core.length-1].substring(0,core[core.length-1].length()-1);
   for(int i = 0; i <core.length;i++) {
     returnString += core[i]+"\n";
   }
   return returnString;
 }

 /**
  *  method that takes in values from the command line and passes them into 
  *  the constructor
  *  @param args[0] - the file name
  *  @param args[1] - the company phone number you want
  **/
 public static void main(String[] args) {
  
  
  if (args.length <= 1) {
   System.out.println("\nPlease specify a filename and lookup name:");
   System.out.println("java PhoneLookup <filename> <name>\n");
   System.exit(0);
  }
  String file = args[0]; 
  String keySearch = args[1];
  
  

  if (!(new File(file).exists())) {
   throw new IllegalArgumentException("No such file or directory: " + file);
  }
  PhoneLookup<String,String> test = new PhoneLookup<String,String>
  (keySearch, file, new MyHashTable<String,String>(100));
  //PhoneLookup<String,String> test = new PhoneLookup<String,String>
  //("Meridian","phoneNumbers.txt", new MyHashTable<String,String>(7));
  System.out.println(test.StringParse());


 }



}